﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using WoodManager;
using WoodManager.Header;
using WoodManager.BO;
using System.Data.SqlClient;

namespace WoodManager.UI
{
    public partial class DinhMucVatTucSon : Form
    {
        DataTable tbVan, tbSon, tbDinhMucSon, tbVatTu, tbNhomVT;
        SqlConnection conn;
        DinhMucVatTuSonBO vtsBO = new DinhMucVatTuSonBO();
        bool isLoadForm = false;
        Validator vali = new Validator();

        string sOldVTID = "";
        string sOldIDToHop = "";
        public DinhMucVatTucSon( SqlConnection AdoConn)
        {
            InitializeComponent();
            conn = AdoConn;
            loadDataTreeView();
            loadNhomVatTu();
            isLoadForm = true;
        }   


        private void loadDataTreeView()
        {
            string ret = CommonHeader.NO_ERROR;
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            treeView1.Nodes.Clear();
            ret = vtsBO.getVan(conn,ref tbVan);
            if (ret != CommonHeader.NO_ERROR)
            {
                MessageBox.Show("Error!" + ret);
                return;
            }

            if (tbVan.Rows.Count < 1)
            {
                return;
            }

            //Leve1 : Van
            TreeNode tnLevel1;
            for (int i = 0; i < tbVan.Rows.Count ; i++)
            {
                ret = vtsBO.getSon(conn, ref tbSon, tbVan.Rows[i][WoodManagerHeader.THVS_ID_VAN].ToString());
                if (ret != CommonHeader.NO_ERROR)
                {
                    MessageBox.Show("Error!" + ret);
                    return;
                }

                //Kiem tra xem loại ván đó có mấy kỹ thuật sơn
                if (tbSon.Rows.Count > 0)
                {
                    //Level 2 : Sơn phủ
                    TreeNode[] tnLevel2 = new TreeNode[tbSon.Rows.Count];
                    for (int j = 0; j < tbSon.Rows.Count; j++)
                    {
                        tnLevel2[j] = new TreeNode(tbSon.Rows[j][WoodManagerHeader.THVS_TEN_SON].ToString());
                        //Add name cho node level 2
                        tnLevel2[j].Name = tbSon.Rows[j][WoodManagerHeader.THVS_ID_VAN_SON].ToString();                   	
                    }                    
                    tnLevel1 = new TreeNode(tbVan.Rows[i][WoodManagerHeader.THVS_TEN_VAN].ToString(), tnLevel2);
                    //tbSon.Rows[j][WoodManagerHeader.THVS_TEN_SON].ToString()
                }                        
                else
                {
                    //Neu don hang do kong co hang muc san pham nao
                    tnLevel1 = new TreeNode(tbVan.Rows[i][WoodManagerHeader.THVS_TEN_VAN].ToString());
                }
                tnLevel1.Name = tbVan.Rows[i][WoodManagerHeader.THVS_ID_VAN].ToString();
                treeView1.Nodes.Add(tnLevel1);
            }              
            if (conn.State == ConnectionState.Open)
                conn.Close();
        }

        private void loadNhomVatTu()
        {
            string ret = CommonHeader.NO_ERROR;
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            ret = vtsBO.getNhomVT(conn, ref tbNhomVT);
            if (ret != CommonHeader.NO_ERROR)
            {
                MessageBox.Show("Error!" + ret);
                return;
            }
            cbbNhomVatTu.Data = tbNhomVT;            
        }

        private void cbbNhomVatTu_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isLoadForm)
            {
                try
                {
                    string ret = CommonHeader.NO_ERROR;
                    if (conn.State == ConnectionState.Closed)
                        conn.Open();
                     ret = vtsBO.getVatTu(conn, ref tbVatTu, cbbNhomVatTu.Text);
                    if ( ret != CommonHeader.NO_ERROR )
                    {
                        MessageBox.Show("Error!" + ret);
                        return;
                    }
                    if (tbVatTu.Rows.Count < 1)
                    {
                        cbbVatTuSon.Data = tbVatTu;
                        cbbVatTuSon.Enabled = false;
                        return;
                    }
                    cbbVatTuSon.Data = tbVatTu;
                    cbbVatTuSon.Text = "";
                    cbbVatTuSon.Enabled = true;
                    if (cbbVatTuSon.SelectedIndex > tbVatTu.Rows.Count)
                    {
                        cbbVatTuSon.SelectedIndex = 0;
                    }
                }
                catch
                { 
                    //do nothing
                }
            }

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!vali.checkNumber(txtDinhMuc.Text))
            {
                MessageBox.Show("Giá trị định mức phải là số");
                return;
            }

            if (cbbVatTuSon.SelectedIndex == -1)
            {
                MessageBox.Show("Mã vật tư không hợp lệ");
                return;
            }
            try
            {
                string sInsert = "DinhMucSon_add N'"+ treeView1.SelectedNode.Name +"', N'"+ cbbVatTuSon.Text +"', N'"+ txtDinhMuc.Text +"'";
                SqlCommand sqlInsert = new SqlCommand(sInsert, conn);

                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                sqlInsert.ExecuteNonQuery();
                //loadListVan();                
                conn.Close();
                sOldVTID = cbbVatTuSon.Text;
                LoadDataGrid();
                selectID(sOldVTID);
            }
            catch (SqlException)
            {
                MessageBox.Show("Mã này đã được sử dụng");
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                conn.Close();
            }
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            treeView1.SelectedNode = e.Node;
            LoadDataGrid();
        }

        private void LoadDataGrid()
        {
            if (treeView1.SelectedNode.Level == 1)
            {
                string ret = CommonHeader.NO_ERROR;
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                ret = vtsBO.LoadDataDinhMucSon(conn, treeView1.SelectedNode.Name, ref tbDinhMucSon);
                if (ret != CommonHeader.NO_ERROR)
                {
                    MessageBox.Show("Error!" + ret);
                    return;
                }
                dtgDinhMucSon.DataSource = tbDinhMucSon;
                conn.Close();
                panel2.Enabled = true;
                sOldIDToHop = treeView1.SelectedNode.Name; 
            }
            else
            {
                dtgDinhMucSon.DataSource = null;
                panel2.Enabled = false;
                sOldIDToHop = "";
            }
        }

        private void dtgDinhMucSon_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                selectID ( tbDinhMucSon.Rows[e.RowIndex]["MaVatTuSon"].ToString() );
            }            
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!vali.checkNumber(txtDinhMuc.Text))
            {
                MessageBox.Show("Giá trị định mức phải là số");
                return;
            }

            if (cbbVatTuSon.Text != sOldVTID)
            {
                MessageBox.Show("Mã vật tư không hợp lệ");
                return;
            }
            try
            {
                string sUpdate = "DinhMucSon_Update N'" + sOldIDToHop + "', N'" + sOldVTID + "', N'" + txtDinhMuc.Text + "'";
                SqlCommand sqlUpdate = new SqlCommand(sUpdate, conn);

                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                sqlUpdate.ExecuteNonQuery();
                //loadListVan();
                conn.Close();
                LoadDataGrid();
                selectID(sOldVTID);
            }
            catch (DuplicateNameException ex)
            {
                MessageBox.Show("Mã này đã được sử dụng" + ex.Message);
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                conn.Close();
            }
        }

        private void selectID(string sID)
        {
            for (int i = 0; i < dtgDinhMucSon.RowCount; i++)
            {
                if (tbDinhMucSon.Rows[i]["MaVatTuSon"].ToString() == sID)
                {
                    dtgDinhMucSon.CurrentCell = dtgDinhMucSon.Rows[i].Cells[0];
                    cbbNhomVatTu.Text = tbDinhMucSon.Rows[i]["ID89"].ToString();
                    cbbVatTuSon.Text = tbDinhMucSon.Rows[i]["MaVatTuSon"].ToString();
                    txtDinhMuc.Text = tbDinhMucSon.Rows[i]["DinhLuong"].ToString();
                    sOldVTID = cbbVatTuSon.Text;
                    break;
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string sDelete = "DinhMucSon_delete N'" + sOldIDToHop + "', N'" + sOldVTID + "'";
                SqlCommand sqlDelete = new SqlCommand(sDelete, conn);

                if (conn.State == ConnectionState.Closed)
                    conn.Open();
                sqlDelete.ExecuteNonQuery();
                //loadListVan();
                conn.Close();
                LoadDataGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                conn.Close();
            }
        }

    }
}
